package com.smartcar.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.smartcar.admin.mapper.AdminMapper;
import com.smartcar.admin.service.AdminService;
import com.smartcar.common.domain.User;
import com.smartcar.common.domain.request.FindUserVo;
import com.smartcar.common.domain.request.UpdateLoginVo;
import com.smartcar.common.result.Result;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("admin")
@Log4j2
@CrossOrigin(origins = "*", maxAge = 3600)
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private AdminMapper adminMapper;

    /**
     * 根据账号查询用户信息
     */
    @PostMapping("findUserByUserName")
    public Result<User> findUserByUserName(@RequestBody FindUserVo findUserVo) {
        log.info("方法名称：根据用户名查询用户信息，参数：{}", findUserVo);
        User user = adminService.findUserByUserName(findUserVo);
        Result<User> result = Result.success(user);
        log.info("方法名称：根据用户名查询用户信息，结果：{}", JSONObject.toJSONString(result));
        return result;
    }

    @PostMapping("/selectDriverByStatus")
    public List<User> selectDriverByStatus(){
        return adminMapper.selectUserByState();
    }

    /**
     * 添加用户信息
     */
    @PostMapping("insertUser")
    public Result<Integer> insertUser(@RequestBody User user) {
        log.info("方法名称：添加用户信息，参数：{}", user.toString());
        int i = adminService.insertUser(user);
        log.info("方法名称：添加用户信息，结果：{}", i > 0 ? "成功" : "失败");
        return Result.success(i);
    }

    /**
     * 用户名唯一校验
     */
    @PostMapping("verifyUniqueUserName")
    public Result<User> verifyUniqueUserName(@RequestParam("username") String username) {
        log.info("方法名称：添加用户信息，参数：{}", username);
        User user = adminService.verifyUniqueUserName(username);
        Result<User> result = Result.success(user);
        log.info("方法名称：添加用户信息，结果：{}", JSONObject.toJSONString(result));
        return result;
    }

    /**
     * 上传图片
     */
    @PostMapping("uploadPicture")
    public Result<String> uploadPicture(@RequestParam("file") MultipartFile file) {
        log.info("方法名称：上传图片，参数：{}", file.toString());
        String picturePath = adminService.uploadPicture(file);
        log.info("方法名称：上传图片，结果：{}", picturePath);
        return Result.success(picturePath);
    }

    /**
     * 根据手机号查用户信息
     */
    @GetMapping("/findByPhone/{phone}")
    public Result<User> findByPhone(@PathVariable("phone") String phone) {
        log.info("方法名称：手机号查用户信息，参数：{}", phone);
        User byPhone = adminService.findByPhone(phone);
        log.info("方法名称：手机号查用户信息，结果：{}", byPhone);
        return Result.success(byPhone);
    }

    /**
     * 修改
     * 最后一次修改时间
     * 最后一次登录时间
     * 最后一次登录设备
     */
    @PostMapping("updateLoginAdmin")
    public Result<Integer> updateLoginAdmin(@RequestBody UpdateLoginVo updateLoginVo) {
        log.info("方法名称：上传图片，参数：{}", updateLoginVo.toString());
        int i = adminService.updateLogin(updateLoginVo);
        Result<Integer> result = Result.success(i);
        log.info("方法名称：上传图片，结果：{}", result.toString());
        return result;
    }

    /**
     * 修改司机接单状态
     */
    @PostMapping("updateDriverAcceptStatus")
    public Result updateDriverAcceptStatus(@RequestBody User user) {
        log.info("方法名称：修改司机接单状态，参数：{}", JSONObject.toJSONString(user));
        int i = adminService.updateDriverAcceptStatus(user);
        Result result = Result.success(i);
        log.info("方法名称：修改司机接单状态，结果：{}", JSONObject.toJSONString(result));
        return result;
    }

    /**
     * 修改司机车辆，车牌
     */
    @PostMapping("updateDriverCar")
    public Result updateDriverCarType(@RequestBody User user) {
        log.info("方法名称：修改司机车辆，入参：{}", JSONObject.toJSONString(user));
        int i = adminService.updateDriverCar(user);
        Result result = Result.success(i);
        log.info("方法名称：修改司机车辆，入参：{}", JSONObject.toJSONString(result));
        return result;
    }

    /**
     * 修改司机位置信息
     */
    @PostMapping("updateDriverAddr")
    public Result updateDriverAddr(@RequestBody User user) {
        log.info("方法名称：修改司机位置信息，入参：{}", JSONObject.toJSONString(user));
        int i = adminService.updateDriverAddr(user);
        Result result = Result.success(i);
        log.info("方法名称：修改司机位置信息，出参", JSONObject.toJSONString(user));
        return result;
    }

}
